console.log('injected');
// 会注入到页面中，开发阶段可以写入mannifest.json中，独立的winow环境，与页面脚本不会发生冲突
if (!window.isInjected) {
    window.isInjected = true;
    class Co {

        contentVersion = '0.0.2';

        getElementsByXpath = (xpathExpression) => {
            const xpathResult = document.evaluate(
                xpathExpression,
                document,
                null,
                XPathResult.ANY_TYPE,
                null,
            );
            let element = xpathResult.iterateNext();
            const elements = []
            while (element) {
                elements.push(element)
                element = xpathResult.iterateNext();
            }
            return elements;
        }

        actions = {
            getRect: (payload) => {
                if (payload.xpath) {
                    const [element] = this.getElementsByXpath(payload.xpath);
                    const rect = element.getBoundingClientRect();
                    return {
                        isSuccessed: true,
                        value: JSON.stringify(rect),
                    }
                }
                return {
                    isSuccessed: false,
                    value: null
                }
            }
        }

        invoke = (action) => {
            return new Promise((resolve) => {
                setTimeout(() => {
                    const res = this.actions[action.type](action.payload)
                    resolve(res)
                }, 2000)
            })
        }
    }
    window.co = new Co()
}
